package com.xz.courseschedule.controller;

import com.xz.courseschedule.mapper.SemesterMapper;
import com.xz.courseschedule.service.UploadService;
import com.xz.courseschedule.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.URLEncoder;

/**
 * 文件上传下载控制器
 * @Author: zys
 * @CreateTime: 2025-05-23
 */
@Tag(name = "文件管理", description = "Excel文件的上传导入和模板下载")
@RestController
public class UploadController{

    @Resource
    private UploadService uploadService;

    @Operation(summary = "上传课程任务文件", description = "上传包含课程任务信息的Excel文件进行批量导入")
    @PostMapping("/uploadTask")
    public Result uploadTaskFile(@Parameter(description = "课程任务Excel文件") MultipartFile file) {
        System.out.println("上传Excl文件(待排课文件)......");
        return uploadService.uploadTask(file);
    }

    @Operation(summary = "上传班级信息文件", description = "上传包含班级信息的Excel文件进行批量导入")
    @PostMapping("/uploadClass")
    public Result uploadClassesFile(@Parameter(description = "班级信息Excel文件") MultipartFile file) {
        System.out.println("上传Excl文件(待班级文件)......");
        return uploadService.uploadClass(file);
    }

    @Operation(summary = "上传教室信息文件", description = "上传包含教室信息的Excel文件进行批量导入")
    @PostMapping("/uploadRoom")
    public Result uploadRoomFile(@Parameter(description = "教室信息Excel文件") MultipartFile file) {
        System.out.println("上传Excl文件(待教室文件)......");
        return uploadService.uploadRoom(file);
    }

    @Operation(summary = "下载班级导入模板", description = "下载班级信息导入的Excel模板文件")
    @GetMapping(value = "/downloadClass",consumes = MediaType.ALL_VALUE)
    public void downloadClassTemplate(HttpServletResponse response){
        File file = new File("doc/班级导入模板.xls");
        String fileName = file.getName();
        response.reset();
        // 设置ContentType，响应内容为二进制数据流，编码为utf-8，此处设定的编码是文件内容的编码
        response.setContentType("application/octet-stream;charset=utf-8");
        response.setCharacterEncoding("UTF-8");

        try {
            String encodedFileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-Disposition", "attachment;filename=" + encodedFileName + ";filename*=UTF-8''" + encodedFileName);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //实现文件下载
        byte[] bytes = new byte[1024];
        try (
            FileInputStream fis = new FileInputStream(file);
            BufferedInputStream bis = new BufferedInputStream(fis)
        ) {
            // 获取字节流
            OutputStream os = response.getOutputStream();
            int i = bis.read(bytes);
            while (i != -1) {
                os.write(bytes, 0, i);
                i = bis.read(bytes);
            }
            System.out.println("文件下载成功");
        } catch (Exception e) {
            System.out.println("文件下载失败:"+ e.getMessage());
        }
    }

    @Operation(summary = "下载教室导入模板", description = "下载教室信息导入的Excel模板文件")
    @GetMapping(value = "/downloadRoom",consumes = MediaType.ALL_VALUE)
    public void downloadRoomTemplate(HttpServletResponse response){
        File file = new File("doc/教室导入模板.xls");
        String fileName = file.getName();
        response.reset();
        // 设置ContentType，响应内容为二进制数据流，编码为utf-8，此处设定的编码是文件内容的编码
        response.setContentType("application/octet-stream;charset=utf-8");
        response.setCharacterEncoding("UTF-8");

        try {
            String encodedFileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-Disposition", "attachment;filename=" + encodedFileName + ";filename*=UTF-8''" + encodedFileName);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //实现文件下载
        byte[] bytes = new byte[1024];
        try (
                FileInputStream fis = new FileInputStream(file);
                BufferedInputStream bis = new BufferedInputStream(fis)
        ) {
            // 获取字节流
            OutputStream os = response.getOutputStream();
            int i = bis.read(bytes);
            while (i != -1) {
                os.write(bytes, 0, i);
                i = bis.read(bytes);
            }
            System.out.println("文件下载成功");
        } catch (Exception e) {
            System.out.println("文件下载失败:"+ e.getMessage());
        }
    }

    @Operation(summary = "下载课程任务导入模板", description = "下载课程任务导入的Excel模板文件")
    @GetMapping(value = "/downloadTask",consumes = MediaType.ALL_VALUE)
    public void downloadTaskTemplate(HttpServletResponse response){
        File file = new File("doc/课程任务导入模板.xls");
        String fileName = file.getName();
        response.reset();
        // 设置ContentType，响应内容为二进制数据流，编码为utf-8，此处设定的编码是文件内容的编码
        response.setContentType("application/octet-stream;charset=utf-8");
        response.setCharacterEncoding("UTF-8");

        try {
            String encodedFileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-Disposition", "attachment;filename=" + encodedFileName + ";filename*=UTF-8''" + encodedFileName);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //实现文件下载
        byte[] bytes = new byte[1024];
        try (
                FileInputStream fis = new FileInputStream(file);
                BufferedInputStream bis = new BufferedInputStream(fis)
        ) {
            // 获取字节流
            OutputStream os = response.getOutputStream();
            int i = bis.read(bytes);
            while (i != -1) {
                os.write(bytes, 0, i);
                i = bis.read(bytes);
            }
            System.out.println("文件下载成功");
        } catch (Exception e) {
            System.out.println("文件下载失败:"+ e.getMessage());
        }
    }
}
